package com.zzyl.nursing.controller;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 护理任务Controller
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务的接口")
public class NursingTaskController extends BaseController {
    @Autowired
    private INursingTaskService nursingTaskService;

    /**
     * 查询护理任务列表
     */
    @ApiOperation("查询护理任务列表")
    @GetMapping("/list")
    public TableDataInfo list(NursingTaskDto nursingTaskDto) {
        startPage();
        List<NursingTaskVo> list = nursingTaskService.selectNursingTaskList(nursingTaskDto);
        return getDataTable(list);
    }

    /**
     * 任务改期
     * <p>
     * * @param requestMap 包含编辑信息的映射对象，键通常为字段名，值为对应的字段值
     * * @return 返回AjaxResult对象，包含编辑操作的结果信息
     */
    @ApiOperation("任务改期")
    @Log(title = "任务改期", businessType = BusinessType.UPDATE)
    @PutMapping("/updateTime")
    public AjaxResult edit(@ApiParam(value = "护理任务实体")
                           @RequestBody Map<String, String> requestMap) {
        // 创建一个NursingTask对象以处理护理任务信息
        NursingTask nursingTask = new NursingTask();

// 设置任务ID，这是从请求参数中获取的
        nursingTask.setId(Long.parseLong(requestMap.get("taskId")));

// 设置预估服务时间，格式为年-月-日 时:分:秒
// 这里使用DateTimeFormatter来解析请求参数中的时间字符串
        nursingTask.setEstimatedServerTime(
                LocalDateTime.parse(requestMap.get("estimatedServerTime"),
                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

// 调用服务层方法更新护理任务信息，并将结果转换为Ajax响应返回
// 这里的toAjax方法用于根据updateNursingTask的结果生成相应的Ajax响应对象
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));


    }

    /**
     * 取消护理任务
     */
    @ApiOperation("取消护理任务")
    @Log(title = "取消护理任务", businessType = BusinessType.UPDATE)
    @PutMapping("cancel")
    public AjaxResult cancelTask(@ApiParam(value = "护理任务实体")
                                 @RequestBody Map<String, String> requestMap) {
        // 创建一个NursingTask对象以处理护理任务信息
        NursingTask nursingTask = new NursingTask();
        // 设置取消原因，这是从请求参数中获取的
        nursingTask.setCancelReason(requestMap.get("reason"));
        // 设置任务ID，这是从请求参数中获取的
        nursingTask.setId(Long.parseLong(requestMap.get("taskId")));
        //  设置任务状态为已取消
        nursingTask.setStatus(3);
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }


    /**
     * 取消护理任务
     */
    @ApiOperation("护理任务执行")
    @Log(title = "护理任务执行", businessType = BusinessType.UPDATE)
    @PutMapping("do")
    public AjaxResult doTask(@ApiParam(value = "护理任务实体")
                             @RequestBody Map<String, String> requestMap) {
        // 创建一个NursingTask对象以处理护理任务信息
        NursingTask nursingTask = new NursingTask();
        // 设置执行时间，这是从请求参数中获取的
        nursingTask.setRealServerTime(
                LocalDateTime.parse(requestMap.get("estimatedServerTime"),
                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        // 设置执行记录
        nursingTask.setMark(requestMap.get("mark"));
        // 设置任务ID
        nursingTask.setId(Long.parseLong(requestMap.get("taskId")));
        // 设置任务图片
        nursingTask.setTaskImage(requestMap.get("taskImage"));
        //  设置任务状态为已执行
        nursingTask.setStatus(2);
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }


    /**
     * 导出护理任务列表
     */
<<<<<<< dev-yjc
 /*   @ApiOperation("导出护理任务列表")
    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NursingTask nursingTask)
    {
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
        util.exportExcel(response, list, "护理任务数据");


    /**
     * 获取护理任务详细信息
     */
    @ApiOperation("获取护理任务详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "护理任务ID", required = true)
                              @PathVariable("id") Long id) {
        return success(nursingTaskService.selectNursingTaskById(id));
    }

    /**
     * 新增护理任务
     */
    @ApiOperation("新增护理任务")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "护理任务实体")
                          @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @ApiOperation("修改护理任务")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateTime(@ApiParam(value = "护理任务实体")
                                 @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }


    /**
     * 删除护理任务
     */
    @ApiOperation("删除护理任务")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }
}
